import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import HomeView from "../views/HomeView.vue";
import store from "@/store";

const routes: Array<RouteRecordRaw> = [
  {
    path: "/home",
    name: "home",
    component: HomeView,
  },
  {
    path: "/about",
    name: "about",
    component: () => import("../views/AboutView.vue"),
  },
  {
    path: "/fst",
    name: "fst",
    component: () => import("../views/FirstView.vue"),
  },
  {
    path: "/sec",
    name: "sec",
    component: () => import("../views/SecondView.vue"),
  },
  {
    path: "/http",
    name: "http",
    component: () => import("../views/HttpView.vue"),
  },
  {
    path: "/httpNew",
    name: "httpNew",
    component: () => import("../views/HttpNew.vue"),
  },
  {
    path: "/httpOld",
    name: "httpOld",
    component: () => import("../views/HttpOld.vue"),
  },
  {
    path: "/leaveApplier",
    name: "leaveApplier",
    component: () => import("../views/LeaveApplier.vue"),
  },
  {
    path: "/leaveAuditor",
    name: "leaveAuditor",
    component: () => import("../views/LeaveAuditor.vue"),
  },
];

const routesEx:Array<RouteRecordRaw> = [

  {
    path: "/login",
    name: "loginPage",
    component: () => import("../views/LoginPage.vue"),
  },
  {
    path: "/",
    redirect:"/login"
  },
  {
    path: "/appPage",
    name: "appPage",
    component: () => import("../views/AppPage.vue"),
    children:routes
  },
]
const router = createRouter({
  history: createWebHashHistory(),
  routes:routesEx,
});

const white=['/','/login']

// 钩子函数
router.beforeEach((to,from,next)=>{
    // console.log(to,from);
    if(white.includes(to.path)){
      next();
      return;
    }
    if(store.state.token === ""){
      next("/login")
      return;
    }

    next();
    return;
    // 放行 
    // next();

})


export default router;
//  ? router?  router.push("/")
//    route    route.path
